#include "udpClient.hpp"

using namespace Client;

//demo 1
void dictHander(int sockfd = 0)
{
    char recv[1024];
    sockaddr_in tmp;
    socklen_t tmp_len = sizeof(tmp);
    ssize_t n = recvfrom(sockfd, recv, sizeof(recv)-1, 0, (sockaddr *)&tmp, &tmp_len);

    if (n >= 0)
    {
        recv[n] = 0;
    }

    cout << "服务器的翻译结果# " << recv << endl;
}

//demo 2
void exceCmd(int sockfd = 0)
{
    char recv[1024];
    sockaddr_in tmp;
    socklen_t tmp_len = sizeof(tmp);
    ssize_t n = recvfrom(sockfd, recv, sizeof(recv)-1, 0, (sockaddr *)&tmp, &tmp_len);

    if (n >= 0)
    {
        recv[n] = 0;
    }

    cout << recv << endl;
}

//demo 3
void chatWithEver(int sockfd = 0)
{
    char recv[1024];
    sockaddr_in tmp;
    socklen_t tmp_len = sizeof(tmp);
    ssize_t n = recvfrom(sockfd, recv, sizeof(recv)-1, 0, (sockaddr *)&tmp, &tmp_len);
    if (n >= 0)
    {
        recv[n] = 0;
    }

    cout << recv << endl;
}

inline void Usage(std::string proc)
{
    std::cerr << "Usage : \n\t" << proc << " server_ip server_port\n\n";
}

// ./udpClient ip port
int main(int argc, char* argv[])
{

    if (argc != 3)
    {
        Usage(argv[0]);
        exit(USAGE_ERROR);
    }

    string server_ip = argv[1];
    uint16_t server_port = atoi(argv[2]);

    std::unique_ptr<udpClient> usrc(new udpClient(server_port, server_ip, chatWithEver));

    usrc->init();
    usrc->run();

    return 0;
}